/**
 * @Title: print.java
 * @Package com.rtmart.txd.barcode
 * @Description: 
 * @author Phoenix 
 * @version V1.0
 */
package com.rtmart.txd.barcode.print;

//import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.swing.JOptionPane;

import com.rtmart.txd.barcode.TxdBarcodePrintUI;

/**
 * @ClassName: print
 * @Description:
 * @author Phoenix
 * @date 2018年3月12日 
 *
 */
public class Print {
	
	private static String[] BarcodeType = {"WLX","D","GH","DD","","","DD",""};
	private static String[] StrFormat = {"%06d","%03d","%06d","%04d","","%d","%06d","%08d"};
	private static String[] DianCangLou = {"1F","2F","3F","1B","2B"};
	private static String[] PrtNameList = {"rail3","rail4","zba2"};
	private String prt;
	public String prtBarcode;
	private ZplPrinter p = null;
	
	public Print() {
		
	}
	public Print(String s) {
		this.p = new ZplPrinter();
	}
	/**
	 * @Title: barcodePrint
	 * @Description: 打印料位条码 zpl语言编写，适合zpl语言兼容打印机zebra，tsc等，目前门店条码机都支持。
	 * @param @param barSeq 条码序号
	 * @param @param barType  条码类型，对应comboBox的index 
	 * @return boolean   
	 * @author Phoenix
	 * @date 2018年3月13日 下午3:33:10
	 */
	public boolean barcodePrint(int barSeq, int barType,int prtSeq) {
//		ZplPrinter p = new ZplPrinter("\\\\10.10.1.37\\tsc2");
		prt = setPrinter(prtSeq);
//		ZplPrinter p = new ZplPrinter(prt);
//		 ZplPrinter p = new ZplPrinter("\\\\127.0.0.1\\tsc2");
//		 1.打印单个条码

		try {
			prtBarcode = getBarcode(barSeq,barType);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, "获取店别失败！");
			e.printStackTrace();
		}

		String barFormat = "^FO080,060^BY2,2.0,160^BCN,,N,N,N^FD${data}^FS";// 条码样式模板
		p.setBarcode(prtBarcode, barFormat);
		p.setChar(prtBarcode, 90, 230, 48, 48);
		String zpl = p.getZpl();
		System.out.println(zpl);
		boolean result1 = p.print(zpl);// 打印
		if (result1)
			System.out.println("打印完毕!");
		p.resetZpl();//注意要清除上次的打印信息
		return result1;
		
//		2.打印中、英、数字、条码混合
//		 左边的条码
//		String bar1 = "07";
//		p.setChar(bar1, 190, 130, 60, 60);
//		String bar1Zpl = "^FO100,200^BY8,3.0,240^BCR,,N,N,N^FD${data}^FS";//条码样式模板
//		p.setBarcode(bar1,bar1Zpl);
//		下边的条码
//		String bar2 = "00000999990018822969";//20位
//		String bar2Paper = "^FO380,600^BY3,3.0,100^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
//		p.setBarcode(bar2,bar2Paper);
//
//		p.setText("国药控股湖南有限公司", 380, 40, 60, 60, 30, 2, 2, 24);
//		p.setChar("CSS0BPKPPR", 380, 100, 60, 60);
//		
//		p.setText("09件",940, 80, 60, 60, 30, 2, 2, 24);
//		p.setText("补", 1100, 80, 60, 60, 30, 2, 2, 24);
//		
//		p.setText("公司自配送 公路", 380, 180, 80, 80, 30, 3, 3, 24);
//		p.setChar("03231151",940, 187, 40, 40);
//		p.setChar("2015-10-10",940, 227, 30, 30);
//		
//		p.setText("湖南六谷大药房连锁有限公司", 380, 260, 60, 60, 30, 2, 2, 24);
//		
//		p.setText("长沙市开福区捞刀河镇中岭村258号", 380, 320, 60, 60, 30, 2, 2, 22);
//		
//		p.setText("多SKU", 800, 485, 60, 60, 30, 2, 2, 24);
//		
//		p.setText("库位:49", 380, 420, 56, 56, 30, 2, 2, 24);
//		p.setText("品类:感冒胶囊", 380, 485, 56, 56, 30, 2, 2, 24);
//		
//		p.setText("批号:", 380, 550, 56, 56, 30, 2, 2, 24);
//		p.setChar("78787878788", 500, 560, 40, 40);
//		String zpl2 = p.getZpl();
//		System.out.println(zpl2);
//		boolean result2 = p.print(zpl2);
	}
	
	/**
	 * @Title: barcodePrint
	 * @Description: 打印料位条码
	 * @param @param JianHuoQu
	 * @param @param ZouDao
	 * @param @param Bei
	 * @param @param LiaoWei   
	 * @return boolean   
	 * @author Phoenix
	 * @date 2018年3月20日 上午10:30:09
	 */
	public boolean barcodePrint(String JianHuoQu ,int ZouDao ,int Bei ,int LiaoWei,int prtSeq) {
//		ZplPrinter p = new ZplPrinter("\\\\10.10.1.37\\tsc2");
		prt = setPrinter(prtSeq);
//		ZplPrinter p = new ZplPrinter(prt);
//		 1.打印单个条码

		prtBarcode = getBarcode(JianHuoQu,ZouDao,Bei,LiaoWei);

		String barFormat = "^FO120,060^BY2,2.0,160^BCN,,N,N,N^FD${data}^FS";// 条码样式模板
		p.setBarcode(prtBarcode, barFormat);
	
		p.setChar(prtBarcode, 120, 230, 48, 48);
		String zpl = p.getZpl();
		System.out.println(zpl);
		boolean result1 = p.print(zpl);// 打印
		if (result1)
			System.out.println("打印完毕!");
		p.resetZpl();
		return result1;
	}
	public boolean barcodePrintDianCang(int kuQu,int louCengIndex,int tongDao,int bei,int ceng,int ge,int prtSeq) {
		
		prt = setPrinter(prtSeq);
//		ZplPrinter p = new ZplPrinter(prt);
		
		prtBarcode = getBarcode(kuQu,louCengIndex,tongDao,bei,ceng,ge);
		String barFormat = "^FO120,060^BY2,2.0,160^BCN,,N,N,N^FD${data}^FS";
		p.setBarcode(prtBarcode, barFormat);
		p.setChar(prtBarcode, 120, 230, 48, 48);
		String zpl = p.getZpl();
		System.out.println(zpl);
		boolean result1 = p.print(zpl);
		if (result1)
			System.out.println("打印完毕!");
		p.resetZpl();
		return result1;
	}
	public boolean barcodePrintDianCang(String bar,int prtSeq) {
		
		prt = setPrinter(prtSeq);
//		ZplPrinter p = new ZplPrinter(prt);
		
		prtBarcode = bar;
		String barFormat = "^FO120,060^BY2,2.0,160^BCN,,N,N,N^FD${data}^FS";
		p.setBarcode(prtBarcode, barFormat);
		p.setChar(prtBarcode, 120, 230, 48, 48);
		String zpl = p.getZpl();
		System.out.println(zpl);
		boolean result1 = p.print(zpl);
		if (result1)
			System.out.println("打印完毕!");
		p.resetZpl();
		return result1;
	}

	public boolean barcodePrintZaiJu(int barSeq,String zaiJuLeiXing,int prtSeq) {
		
		prt = setPrinter(prtSeq);
//		ZplPrinter p = new ZplPrinter(prt);
		
		try {
			prtBarcode = getBarcodeZaiJu(barSeq,zaiJuLeiXing);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String barFormat = "^FO120,060^BY2,2.0,160^BCN,,N,N,N^FD${data}^FS";
		p.setBarcode(prtBarcode, barFormat);
		p.setChar(prtBarcode, 120, 230, 48, 48);
		String zpl = p.getZpl();
		System.out.println(zpl);
		boolean result1 = p.print(zpl);
		if (result1)
			System.out.println("打印完毕!");
		p.resetZpl();
		return result1;
	}
	
	public boolean barcodePrintMMCZaiJu(int barSeq,String zaiJuLeiXing,int prtSeq) {
		
		prt = setPrinter(prtSeq);
//		ZplPrinter p = new ZplPrinter(prt);
		
		try {
			prtBarcode = getBarcodeMMCZaiJu(barSeq,zaiJuLeiXing);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String barFormat = "^FO120,060^BY2,2.0,160^BCN,,N,N,N^FD${data}^FS";
		p.setBarcode(prtBarcode, barFormat);
		p.setChar(prtBarcode, 120, 230, 48, 48);
		String zpl = p.getZpl();
		System.out.println(zpl);
		boolean result1 = p.print(zpl);
		if (result1)
			System.out.println("打印完毕!");
		p.resetZpl();
		return result1;
	}
	
	public boolean barcodePrint(int barSeq,String wuLiuXiangLeiXing,int prtSeq) {
//		ZplPrinter p = new ZplPrinter("\\\\10.10.1.37\\tsc2");
		prt = setPrinter(prtSeq);
//		ZplPrinter p = new ZplPrinter(prt);
//		 1.打印单个条码

		try {
			prtBarcode = getBarcode(barSeq,wuLiuXiangLeiXing);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			JOptionPane.showMessageDialog(null, "获取店别失败！");
			e.printStackTrace();
		}

		String barFormat = "^FO120,060^BY2,2.0,160^BCN,,N,N,N^FD${data}^FS";// 条码样式模板
		p.setBarcode(prtBarcode, barFormat);
	
		p.setChar(prtBarcode, 120, 230, 48, 48);
		String zpl = p.getZpl();
		System.out.println(zpl);
		boolean result1 = p.print(zpl);// 打印
		if (result1)
			System.out.println("打印完毕!");
		p.resetZpl();
		return result1;
	}
	/**
	 * @Title: getBarcode
	 * @Description: 根据规则合成非料位条码
	 * @param barSeq 条码序号
	 * @param barType 条码类型
	 * @throws UnknownHostException   
	 * @return String   
	 * @author Phoenix
	 * @throws UnknownHostException 
	 * @date 2018年3月13日 下午3:34:11
	 */
	public String getBarcode(int barSeq,int barType) throws UnknownHostException {
	    String rtnBarcode;
	    
	    switch (barType){
	    case 5:
	    	rtnBarcode = String.format(StrFormat[barType], barSeq);
	    	break;
	    case 6:
	    	rtnBarcode = BarcodeType[barType] + String.format(StrFormat[barType], barSeq);
	    	break;
	    case 7:
	    	rtnBarcode = "DRF" + getStore() + String.format(StrFormat[barType],barSeq) + "YX";
	    	break;
	    default:
	    	rtnBarcode = getStore() + BarcodeType[barType] + String.format(StrFormat[barType], barSeq);
	    	break;
	    }

		System.out.println(rtnBarcode);
		return rtnBarcode;
	}
	
	
	/**
	 * @Title: getBarcode
	 * @Description: 合成料位条码
	 * @param JianHuoQu
	 * @param ZouDao
	 * @param Bei
	 * @param LiaoWei   
	 * @return String   
	 * @author Phoenix
	 * @date 2018年3月20日 上午9:59:06
	 */
	public String getBarcode(String JianHuoQu,int ZouDao,int Bei,int LiaoWei ) {
		String rtnBarcode;
		
		rtnBarcode = JianHuoQu.toUpperCase() + "-" + String.format("%02d",ZouDao) + "-" + String.format("%02d", Bei) + "-" + String.format("%02d", LiaoWei);
		System.out.println(rtnBarcode);
		return rtnBarcode;
	}
	
	public String getBarcode(int kuQu,int louCengIndex,int tongDao,int bei,int ceng ,int ge) {
		String rtnBarcode;
		rtnBarcode = String.format("%02d", kuQu)+"-"+ DianCangLou[louCengIndex]+"-"+String.format("%03d", tongDao)+String.format("%02d", bei)+String.format("%1d", ceng)+String.format("%1d", ge) ;
		return rtnBarcode;
	}
	public String getBarcode(int barSeq,String wuLiuXiangLeiXing) throws UnknownHostException {
		String rtnBarcode;
		
		rtnBarcode = "DRF" + getStore() + wuLiuXiangLeiXing + String.format("%07d", barSeq);
		System.out.println(rtnBarcode);
		return rtnBarcode;
	}
	public String getBarcodeZaiJu(int barSeq,String zaiJuLeiXing) throws UnknownHostException {
		String rtnBarcode;
		
		rtnBarcode = getStore() + zaiJuLeiXing + String.format("%06d", barSeq);
		System.out.println(rtnBarcode);
		return rtnBarcode;
	}
	public String getBarcodeMMCZaiJu(int barSeq,String zaiJuLeiXing) throws UnknownHostException {
		String rtnBarcode;
		
		rtnBarcode = "RT-" + zaiJuLeiXing.toUpperCase() + getStore() + String.format("%04d", barSeq);
		System.out.println(rtnBarcode);
		return rtnBarcode;
	}
	public String setPrinter(int printSeq) {
		String rtnPrinter;
		
		rtnPrinter = PrtNameList[printSeq];
		System.out.println(rtnPrinter);
		return rtnPrinter;
	}
	
	public String getStore() throws UnknownHostException {
//	    int storeArea=0;
//	    int storeNo=0;
//	    int store;	    
//		InetAddress ia = InetAddress.getLocalHost();
//		ip前3段10.10.1则为总公司开发环境
//      使用环境为172.xx.xx.xx网段设备
//		if (Byte.toUnsignedInt(ia.getAddress()[0]) == 10 && Byte.toUnsignedInt(ia.getAddress()[1]) == 10 && Byte.toUnsignedInt(ia.getAddress()[2]) == 1) {
//			System.out.println("ho01开发环境");
//			store = 1001;
//		} 
//		else {
//			switch (ia.getAddress()[1]) {
//			case 16:
//			case 26:
//			case 36:
//			case 46:
//			case 56:
//				storeArea = 1;
//				break;
//			case 17:
//			case 27:
//			case 37:
//			case 47:
//			case 57:
//				storeArea = 2;
//				break;
//			case 18:
//			case 28:
//			case 38:
//			case 48:
//			case 58:
//				storeArea = 3;
//				break;
//			case 19:
//			case 29:
//			case 39:
//			case 49:
//			case 59:
//				storeArea = 4;
//				break;
//			case 20:
//			case 30:
//			case 40:
//			case 50:
//			case 60:
//				storeArea = 5;
//				break;
//			default:
//				storeArea = 1;
//			}
//			storeNo = (Byte.toUnsignedInt(ia.getAddress()[1]) - 16) / 10 * 254 + Byte.toUnsignedInt(ia.getAddress()[2]);
//			store = storeArea * 1000 + storeNo;
//		}
//		return Integer.toString(store);
		return TxdBarcodePrintUI.textStoreNo.getText();

	}
	
}
